package com.tweetgraph.controller;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.google.appengine.api.taskqueue.Queue;
import com.google.appengine.api.taskqueue.QueueFactory;
import com.google.appengine.api.taskqueue.TaskOptions;
import com.tweetgraph.domain.BasicQuery;
import com.tweetgraph.util.TGUtil;

@SuppressWarnings("serial")
public class QueryProcessor extends HttpServlet {


	public void service(HttpServletRequest req, HttpServletResponse resp) throws IOException, ServletException{

		
		BasicQuery query = extractQueryFromReq(req);
		
		if(query != null){
			TGUtil.saveQuery(query);
//			TGUtil.printQueryDetails(query.getHashTag());
			
			
			/*
			 * Start the task.
			 */
			invokeTask(query.getHashTag());
			
			/*
			 * Send to the graph.jsp
			 */
			req.setAttribute("hashTag", query.getHashTag());
			req.setAttribute("name", query.getName());
			
			req.getRequestDispatcher("graph.jsp").forward(req, resp);
		}else{
			try{
				PrintWriter writer = resp.getWriter();
				writer.write("<b> Under Development </b>");
				writer.flush();
			}catch(Exception e){
				e.printStackTrace();
			}
		}
        
	}

	
	private void invokeTask(String hashTag){
		Queue queue = QueueFactory.getDefaultQueue();
		queue.add(TaskOptions.Builder.withUrl("/twittersearch").param("hashTag",hashTag));
		
	}
	
	
	
	private BasicQuery extractQueryFromReq(HttpServletRequest req){
		String mode = req.getParameter("mode");

		/*
		 * pie and bar will be going to the same page as of now.
		 * Geo will go to a different page.
		 *
		 * Start the TwitterQuery job thread -> which will be updating the Datastore with the
		 * query object.
		 */
		String qName = null;
		String qHash = null;
		String qOther = null;
		String qOption1 = null;
		String qOption2 = null;
		String qOption3 = null;



		if("pie".equals(mode)){
			qName = req.getParameter("qPieName");
			qHash = req.getParameter("qPieHash");
			qOther = req.getParameter("qPieOther");

			qOption1 = "Yes";
			qOption2 = "No";


		}else if("bar".equals(mode)){
			qName = req.getParameter("qBarName");
			qHash = req.getParameter("qBarHash");
			qOther = req.getParameter("qBarOther");

			qOption1 = req.getParameter("qBarOption1");
			qOption2 = req.getParameter("qBarOption2");
			qOption3 = req.getParameter("qBarOption3");


		}
		else if("geo".equals(mode)){
//			qName = req.getParameter("qGeoName");
//			qHash = req.getParameter("qGeoHash");
			return null;

		}


		/*
		 * Form the query object - with the properties set
		 */
		BasicQuery query = new BasicQuery(qName,qHash);
		
		if(qOption1 != null){
			query.addKeyWord(qOption1);
		}
		
		if(qOption2 != null){
			query.addKeyWord(qOption2);
		}
		
		if(qOption3 != null){
			query.addKeyWord(qOption3);
		}

		if(null != qOther && 	"yes".equals(qOther.toLowerCase())){
			query.setCountOther(true);
		}
		
		if("pie".equals(mode)){
			query.setType("pie");
		}else if("bar".equals(mode)){
			query.setType("bar");
		}
		
		return query;
	}

}
